package common.config;

import cn.dreampie.shiro.core.ShiroPlugin;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import common.interceptor.CatchAccountingExceptionInterceptor;
import common.interceptor.FreemarkerGlobalParameter;
import common.interceptor.TestAutoLogin;
import core.jfinal.ext.autoroute.AutoBindRoutes;

import java.net.URL;

/**
 * Created by Administrator on 2017/3/3.
 */
public class MainConfig extends JFinalConfig {
    Routes routes;

    public void configConstant(Constants constants) {
        constants.setDevMode(true);
        constants.setBaseViewPath("WEB-INF/page");
        FreeMarkerRender.setProperty("classic_compatible", "true");
    }

    public void configRoute(Routes routes) {
        this.routes = routes;
        routes.add(new AutoBindRoutes());

    }




    public void configPlugin(Plugins plugins) {
        //配置shiro插件
        plugins.add(new ShiroPlugin(routes));
        //配置Ehcache插件
        plugins.add(new EhCachePlugin(MainConfig.class.getClassLoader().getResource("ehcache.xml").getFile()));



        //配置数据源
        DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost/jfinal?useUnicode=true&characterEncoding=utf-8", "root", "");
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        arp.setShowSql(true);
        plugins.add(dp);
        plugins.add(arp);
    }

    public void configInterceptor(Interceptors interceptors) {
        interceptors.addGlobalActionInterceptor(new TestAutoLogin())
                .addGlobalActionInterceptor(new CatchAccountingExceptionInterceptor())
                .addGlobalActionInterceptor(new FreemarkerGlobalParameter());
    }

    public void configHandler(Handlers handlers) {

    }

    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 8090,"/", 5);
    }
}
